package com.plc.schedu;

import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpResponse;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import cn.hutool.log.level.Level;
import com.github.s7connector.api.S7Connector;
import com.plc.config.PlcConfig;
import com.plc.httputils.SendMESHttpRequestUtils;
import com.plc.s7.ReadPLC;
import com.plc.s7.S7Client;
import com.plc.wmsentity.PlcEntity;
import com.plc.wmsentity.WmsPlcDataEntity;
import lombok.Data;

import java.util.Date;


@Data
public class ScheduPLC {


    Log log = LogFactory.get();
    //零件站ip
    public String plcIp;

    public String plcDB;



    public String mes_ip_address;

    public String mes_port;

    public String mes_token;



    public ScheduPLC(){
        this.plcIp = PlcConfig.getProperty("plc.ip");
        this.plcDB = PlcConfig.getProperty("plc.db");
       this.mes_ip_address =PlcConfig.getProperty("mes.ipaddress");
        this.mes_port =PlcConfig.getProperty("mes.port");
        this.mes_token =PlcConfig.getProperty("mes.token");
    }



    public void lingjianzhanJob(){
//        S7Client s7ClientPLC = new S7Client();
//        log.log(Level.INFO,"日期{},调用plc地址为：{},开始调用========================",DateUtil.now(),linJianZhan_Ip);
//        S7Connector s7Connector = s7ClientPLC.initConnect(linJianZhan_Ip);
//        if(s7Connector!=null) {
//            ReadPLC readPLC = new ReadPLC();
//            //读取
//            PlcEntity plcEntity = readPLC.Cyclic_read(s7Connector, new Integer(linJianZhan_DB));
//            WmsPlcDataEntity plcDataEntity = new WmsPlcDataEntity();
//            plcDataEntity.getEquipmentList().add(plcEntity);
//            //SendMESHttpRequestUtils.sendPost(plcDataEntity, mes_ip_address, mes_token);
//        }else {
//            log.log(Level.INFO,"日期{},调用plc地址为：{},失败,无法连接至PLC服务器",DateUtil.now(),linJianZhan_Ip);
//        }
//
//        log.log(Level.INFO,"日期{},调用plc地址为：{},结束调用========================",DateUtil.now(),linJianZhan_Ip);

    }

    public void plcJob(){
        S7Client s7ClientPLC = new S7Client();
        log.log(Level.INFO,"日期{},调用plc地址为：{},开始调用========================",DateUtil.now(),plcIp);
        S7Connector s7Connector = s7ClientPLC.initConnect(plcIp);
        if(s7Connector!=null) {
            ReadPLC readPLC = new ReadPLC();
            //读取
            PlcEntity plcEntity = readPLC.Cyclic_read(s7Connector, new Integer(plcDB));
            WmsPlcDataEntity plcDataEntity = new WmsPlcDataEntity();
            plcDataEntity.getEquipmentList().add(plcEntity);
            SendMESHttpRequestUtils.sendPost(plcDataEntity, mes_ip_address, mes_token);
        }else {
            log.log(Level.INFO,"日期{},调用plc地址为：{},失败,无法连接至PLC服务器",DateUtil.now(),plcDB);
        }

        log.log(Level.INFO,"日期{},调用plc地址为：{},结束调用========================",DateUtil.now(),plcIp);
    }

}
